package com.coderxq.blog;

import org.apache.commons.lang3.StringUtils;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;

@SpringBootApplication
@ImportResource(locations = {"classpath:spring/application-spring.xml"})
@MapperScan(basePackages = {"com.coderxq.blog", "com.coderxq.blog.mapper"},annotationClass = Repository.class) //扫描被@Repository注解 标注得类
@EnableCaching
@EnableScheduling
@ComponentScan(basePackages ={"com.coderxq.blog"} )
public class BlogApplication {
    private final static Logger log = LoggerFactory.getLogger(BlogApplication.class);

    public static void main( String[] args ) {

        log.info("=================  服务启动中...  =================");

        final ConfigurableApplicationContext applicationContext = SpringApplication.run(BlogApplication.class, args);

        final Environment environment = applicationContext.getBean(Environment.class);

        final String profilesActive = environment.getProperty("spring.profiles.active");

        // 使用的配置环境
        log.info("当前使用的配置环境: 【{}】", profilesActive);

        // 服务端口
        final String port = environment.getProperty("server.port");

        // 服务访问路径
        final String contextPath = environment.getProperty("server.servlet.context-path");

        // 是否开启 Druid监控
        String statViewServlet = environment.getProperty("spring.datasource.druid.stat-view-servlet.enabled");
        if (StringUtils.isBlank(statViewServlet)) {
            statViewServlet = "false";
        }
        log.info("是否开启 Druid 数据库 监控：【{}】", statViewServlet);
        if ("true".equals(statViewServlet)) {
            final String druidUserName = environment.getProperty("spring.datasource.druid.stat-view-servlet.login-username");
            final String druidPassword = environment.getProperty("spring.datasource.druid.stat-view-servlet.login-username");
            log.info("Druid访问路径：【{}druid/login.html】", contextPath);
            log.info("Druid 登录用户名：【{}】", druidUserName);
            log.info("Druid 登录密码：【{}】", druidPassword);
        }
        // 是否开启 actuator 监控
        String statActuator = environment.getProperty("management.endpoint.health.show-details");
        if (StringUtils.isNotBlank(statActuator)) {
            statActuator = "true";
        } else {
            statActuator = "false";
        }
        log.info("是否开启 Aactuator 程序监控：【{}】", statActuator);

        if ("true".equals(statActuator)) {
            log.info("Actuator访问路径：【{}/actuator】", contextPath);
        }

        log.info("是否开启 Druid监控：【{}】", statViewServlet);
        log.info("服务访问路径：【{}】", contextPath);
        log.info("服务访问端口：【{}】", port);
        log.info("服务启动时间：【{}】", LocalDateTime.now());
        System.out.println("redis连接信息：" + environment.getProperty("spring.redis.host") + ":" + environment.getProperty("spring.redis.port"));
        log.info("=================  服务启动成功  =================");
    }


}
